/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cadbury;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class Password extends JPanel implements ActionListener {
    private static String OK = "ok";
    private static String HELP = "help";
    private JFrame controllingFrame; //needed for dialogs
    private JPasswordField passwordField;

    public Password(JFrame f) {
        controllingFrame = f;
        passwordField = new JPasswordField(10);
        passwordField.setActionCommand(OK);
        passwordField.addActionListener(this);
        JComponent buttonPane = createButtonPanel();
        JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        textPane.add(passwordField);
        add(textPane);
        add(buttonPane);
    }

    protected JComponent createButtonPanel() {
        JPanel p = new JPanel(new GridLayout(0, 1));
        JButton okButton = new JButton("OK");
        JButton helpButton = new JButton("Help");

        okButton.setActionCommand(OK);
        helpButton.setActionCommand(HELP);
        okButton.addActionListener(this);
        helpButton.addActionListener(this);

        p.add(okButton);
        p.add(helpButton);

        return p;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (OK.equals(cmd)) { //Process the password.
            char[] input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,"Welcome, Mr. Mavroidis");
                JFrame frame = this.controllingFrame;
                frame.setVisible(false);
                try {
                    Welcome welcomeScreen = new Welcome();
                } catch (IOException ex) {
                    Logger.getLogger(Password.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Password.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                        "Invalid. Try again.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
            }
            //Zero out the possible password, for security.
            Arrays.fill(input, '0');
            passwordField.selectAll();
            resetFocus();
        } else { //The user has asked for help.
            JOptionPane.showMessageDialog(controllingFrame,
                    "Your information is being sent to George\n");
        }
    }

    /**
     * Checks the passed-in array against the correct password. After this
     * method returns, you should invoke eraseArray on the passed-in array.
     */
    private static boolean isPasswordCorrect(char[] input) {
        boolean isCorrect = true;
        String password = "waterloo";
        char[] correctPassword = password.toCharArray();

        if (input.length != correctPassword.length) {
            isCorrect = false;
        } else {
            isCorrect = Arrays.equals(input, correctPassword);
        }

        //Zero out the password.
        Arrays.fill(correctPassword, '0');
        return isCorrect;
    }

    //Must be called from the event dispatch thread.
    protected void resetFocus() {
        passwordField.requestFocusInWindow();
    }
    }
